*************************************************************************
*Table A.12: Heterogeneous effect by machine mode
*This table reports differential estimated treatment effects by machine mode assigned to dealers per month for ration card-level outcomes in endline one (January - March)
*****************************************************************************


use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear

*******************************************************************************

svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)


*************************************************************************
program mydisp, eclass
         matrix betass=e(b)
         matrix colnames betass = "one" "two" "three"
         mat list betass
         ereturn repost b = betass, rename
 end
*************************************************************************


foreach mon in jan17 feb17 mar17{

gen machine_mode_num_`mon'=1 if machine_mode_`mon'=="partialonline"
replace machine_mode_num_`mon'=2 if machine_mode_`mon'=="online"
replace machine_mode_num_`mon'=3 if machine_mode_`mon'=="offline"
}



*generate vars for machine mode (online offline partially_online)

foreach mon in jan17 feb17 mar17{

*online
gen online_`mon'=1 if machine_mode_`mon'=="online"
replace online_`mon'=0 if inlist(machine_mode_`mon',"offline","partialonline")

*offline
gen offline_`mon'=1 if machine_mode_`mon'=="offline"
replace offline_`mon'=0 if inlist(machine_mode_`mon',"online","partialonline")

*partialonline
gen partialonline_`mon'=1 if machine_mode_`mon'=="partialonline"
replace partialonline_`mon'=0 if inlist(machine_mode_`mon',"online","offline")

}




* Keep if surveyed or classified as ghost
keep if ss_code == "SS01" | ghost_final == 1

preserve
tempfile HHdata_withghosts
save `HHdata_withghosts'
restore

*******************************************************************************
*Drop ghost HHs - only use this in receipts
*******************************************************************************
preserve

keep if ss_code == "SS01"
* preserve 
tempfile HHdata_surveyedEL1
save `HHdata_surveyedEL1'
restore


*******************************************************************************


*Outcomes that we have monthly measurement of & want to report unpooled
loc Moutcomes "c_total_access_cost_adj_mar17"
loc BLoutcomesM "c_cost_to_access_y0"

*Outcomes with monthly measurement of and pooled
loc ELoutcomesBLg "value_total"
loc BLoutcomesg "value_total_y0"


*Outcome that we only have monthly measure monthly measurement & no BL lag
loc ELoutcomes "c6_WTA"

******************************************
* access cost (March, with BL)
***********************************************
use `HHdata_surveyedEL1',clear


qui svy: mean c_total_access_cost_adj_mar17 if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

gen bl_var = c_cost_to_access_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

*scalar obs_all = 3*(3960 - ghosts)
scalar obs_all = 3960-ghosts

gen TXonline=treatment*online_mar17
gen TXoffline=treatment*offline_mar17
gen TXpartialonline=treatment*partialonline_mar17


eststo access_cost:reg c_total_access_cost_adj_mar17  TXonline TXoffline TXpartialonline online_mar17 offline_mar17 partialonline_mar17  i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)


estadd scalar percent_obs 100*e(N)/obs_all
estadd scalar control_mean = `=Mean0[1,1]'


drop bl_var* TX*





**************************************
* total value (pooled, with BL)
**************************************

*For now the indicator for caste - need to confirm with field team

preserve

use `HHdata_withghosts',clear


scalar obs_all = 3*3960 


loc ELoutcomesBLg "value_total"
loc BLoutcomesg "value_total_y0"

gen bl_var = value_total_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

loc Ovar "value_total"
loc Hvar "machine_mode_num"
keep `Ovar'_mar17 `Ovar'_feb17 `Ovar'_jan17 treatment  strata pweight block_code uid bl_var bl_var_mi `Hvar'*  online_* partialonline_* offline_*
rename `Ovar'_jan17 `Ovar'1
rename `Ovar'_feb17 `Ovar'2
rename `Ovar'_mar17 `Ovar'3
rename `Hvar'_jan17 `Hvar'1
rename `Hvar'_feb17 `Hvar'2
rename `Hvar'_mar17 `Hvar'3

loc modes "online partialonline offline"

foreach m of local modes{
rename `m'_jan17 `m'1
rename `m'_feb17 `m'2
rename `m'_mar17 `m'3

}


loc Ovar "value_total"
loc Hvar "machine_mode_num"
reshape long `Ovar' `Hvar' online partialonline offline, i(uid) j(month)


gen TXonline=treatment*online
gen TXoffline=treatment*offline
gen TXpartialonline=treatment*partialonline


qui svy: mean value_total if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)


loc Ovar "value_total"
loc Hvar "machine_mode_num"

eststo value_received:reg `Ovar' TXonline TXoffline TXpartialonline  online offline partialonline i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
estadd scalar percent_obs 100*e(N)/obs_all
estadd scalar control_mean = `=Mean0[1,1]'



drop bl_var*
restore



**********************************************
* WTA
*********************************************

preserve

scalar obs_all = 3*(3960 - ghosts)



use `HHdata_surveyedEL1',clear

replace c6_WTA_mar17 = 0 if c6a_no_ration_flag_mar17 == 1
replace c6_WTA_feb17 = 0 if c6a_no_ration_flag_feb17 == 1
replace c6_WTA_jan17 = 0 if c6a_no_ration_flag_jan17 == 1



loc Ovar "c6_WTA"
loc Hvar "machine_mode_num"
keep `Ovar'_mar17 `Ovar'_feb17 `Ovar'_jan17 treatment  strata pweight block_code uid `Hvar'*  online_* partialonline_* offline_*
rename `Ovar'_jan17 `Ovar'1
rename `Ovar'_feb17 `Ovar'2
rename `Ovar'_mar17 `Ovar'3
rename `Hvar'_jan17 `Hvar'1
rename `Hvar'_feb17 `Hvar'2
rename `Hvar'_mar17 `Hvar'3

loc modes "online partialonline offline"

foreach m of local modes{
rename `m'_jan17 `m'1
rename `m'_feb17 `m'2
rename `m'_mar17 `m'3

}
reshape long `Ovar' `Hvar' online partialonline offline, i(uid) j(month)

gen TXonline=treatment*online
gen TXoffline=treatment*offline
gen TXpartialonline=treatment*partialonline


qui svy: mean c6_WTA if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

loc Ovar "c6_WTA"

eststo WTA:reg `Ovar' TXonline TXoffline TXpartialonline online offline partialonline i.strata [pw = pweight], cluster(block_code)
estadd scalar percent_obs 100*e(N)/obs_all
estadd scalar control_mean = `=Mean0[1,1]'


restore


* Output table
#delimit ;
esttab value_received WTA access_cost using "${OutputDir}/TableA_12.tex" , 
	label b(%12.2g) se(%12.2g) booktabs replace nocons nolz width(\hsize)
	drop(_cons  bl*  online* offline* *strata* partial*)
	order (TXonline TXoffline TXpartialonline  bl_var)
	coeflabels(TXonline "Treatment*Online" TXoffline "Treatment*Offline" TXpartialonline "Treatment*Partial")
	stats(r2_a control_mean N percent_obs, 
			labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample"
				) fmt(2 %12.2g 0 0))

	star(* .10 ** .05 *** .01) 
	mgroups("Value received (mkt prices)" "Value received (WTA)" "Transaction costs",	
			pattern(1 1 1)  prefix(\multicolumn{@span}{c}{) suffix(}) 
			span erepeat(\cmidrule(lr){@span}))
	mlabel(none)
	substitute("Standard errors in parentheses" " "
				"\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "
				"\multicolumn{7}{l}{\footnotesize  }\\" " ");

#delimit cr
eststo cr
program drop mydisp
